package UILayer;

import java.util.Scanner;

/**
 * Write a description of class CustomerUI here.
 * 
 * @author (Miroslav Marinov)
 * @version (a version number or a date)
 */
public class CustomerUI
{
	private CustomerCtr customer_ctr;

	/**
	 * Constructor for objects of class CustomerUI
	 */
	public CustomerUI()
	{
		customer_ctr = new CustomerCtr();
	}

	public void start_customer_menu()
	{
		boolean exit = false;
		while (!(exit))
		{
			int choice = write_menu();
			switch (choice)
			{
				case 1:
					add_customer();
					break;
				case 2:
					view_customer();
					break;
				case 3:
					edit_menu(check_id());
					break;
				case 4:
					delete_customer();
					break;
				case 5:
					group_menu();
					break;
				default:
					exit = true;
					customer_ctr.save_data();
					break;
			}
		}
		System.out.println("\f");
	}

	public void edit_menu(String id)
	{
		boolean exit = false;

		while (!(exit))
		{
			int choice = write_edit();
			switch (choice)
			{
				case 1:
					update_name(id);
					break;
				case 2:
					update_phone_nr(id);
					break;
				case 3:
					update_address(id);
					break;
				case 4:
					update_code(id);
					break;
				case 5:
					update_city(id);
					break;
				case 6:
					update_group(id);
					break;
				default:
					exit = true;
					customer_ctr.save_data();
					break;
			}
		}
		System.out.println("\f");
	}

	public void group_menu()
	{
		boolean exit = false;
		while (!(exit))
		{
			if (!(CurrentAccount.give_rank().equals("employee")))
			{
				int choice = write_group_manager();
				switch (choice)
				{
					case 1:
						add_group();
						break;
					case 2:
						edit_group();
						break;
					case 3:
						view_all();
						break;
					case 4:
						print_group();
						break;
					case 5:
						delete_group();
						break;
					default:
						exit = true;
						customer_ctr.save_data();
						break;
				}
			}
			else
			{
				int choice = write_group_employee();
				switch (choice)
				{
					case 1:
						view_all();
						break;
					case 2:
						print_group();
						break;
					default:
						exit = true;
						break;
				}
			}
		}
		System.out.println("\f");
	}

	public int write_menu()
	{
		Scanner keyboard = new Scanner(System.in);
		boolean ok = false;
		int choice = 0;
		System.out.println("\f***CUSTOMERS MENU***");
		System.out.println("Press (1) to add a customer.");
		System.out.println("Press (2) to view Customer's details.");
		System.out.println("Press (3) to update customer's details.");
		System.out.println("Press (4) to delete a customer.");
		System.out.println("Press (5) to manage groups.");
		System.out.println("Press any other number to go back.");
		while (!(ok))
		{
			try
			{
				choice = keyboard.nextInt();
				ok = true;
			}
			catch (Exception e)
			{
				System.out
						.println("You must provide a valid input.\nYou have to type a number");
				keyboard.nextLine();
			}
		}
		keyboard.close();
		return choice;
	}

	public int write_edit()
	{
		Scanner keyboard = new Scanner(System.in);
		boolean ok = false;
		int choice = 0;
		System.out.println("\f***UPDATE DETAILS***");
		System.out.println("Press (1) to update a customer's name.");
		System.out.println("Press (2) to update a customer's phone number.");
		System.out.println("Press (3) to update a customer's address.");
		System.out.println("Press (4) to update a customer's postal code.");
		System.out.println("Press (5) to update a customer's city.");
		System.out.println("Press (6) to update a customer's group.");
		System.out.println("Press any other number to go back.");

		while (!(ok))
		{
			try
			{
				choice = keyboard.nextInt();
				ok = true;
			}
			catch (Exception e)
			{
				System.out
						.println("You must provide a valid input.\nYou have to type a number");
				keyboard.nextLine();
			}
		}
		keyboard.close();
		return choice;
	}

	public int write_group_manager()
	{
		Scanner keyboard = new Scanner(System.in);
		boolean ok = false;
		int choice = 0;

		System.out.println("\f***GROUP DETAILS***");
		System.out.println("Press (1) to add a group.");
		System.out.println("Press (2) to edit a group.");
		System.out.println("Press (3) to view all groups.");
		System.out.println("Press (4) to view a group's details.");
		System.out.println("Press (5) to delete a group.");
		System.out.println("Press any other number to go back.");

		while (!(ok))
		{
			try
			{
				choice = keyboard.nextInt();
				ok = true;
			}
			catch (Exception e)
			{
				System.out
						.println("You must provide a valid input.\nYou have to type a number");
				keyboard.nextLine();
			}
		}
		keyboard.close();
		return choice;
	}

	public int write_group_employee()
	{
		Scanner keyboard = new Scanner(System.in);
		boolean ok = false;
		int choice = 0;

		System.out.println("\f***GROUP DETAILS***");
		System.out.println("Press (1) to view all groups.");
		System.out.println("Press (2) to view a group's details.");
		System.out.println("Press any other number to go back.");

		while (!(ok))
		{
			try
			{
				choice = keyboard.nextInt();
				ok = true;
			}
			catch (Exception e)
			{
				System.out
						.println("You must provide a valid input.\nYou have to type a number");
				keyboard.nextLine();
			}
		}
		keyboard.close();
		return choice;
	}

	public void add_customer()
	{
		if (!customer_ctr.existing_groups())
		{
			System.out.println("Please enter Customer's Personal details:");
			String id = customer_ctr.add_customer(input_name(), input_number(),
					input_address(), input_code(), input_city(), input_group());
			System.out
					.println("You successfully added a customer! His personal details are:\n"
							+ customer_ctr.view_customer(id)
							+ "\nPress ENTER to continue...");
		}
		else
		{
			System.out
					.println("There are no existing groups right now.\nPlease create one first to add a customer...");
		}
		wait_for_input();
	}

	public void view_customer()
	{
		boolean exit = true;
		System.out.println("Please enter desired Customer's ID:");
		String id = wait_for_input();
		if (!customer_ctr.existing_customer(id))
		{
			System.out
					.println("Customer with such ID is not found.\nPress ENTER to continue...");
			exit = false;
		}
		else
		{
			System.out.println(customer_ctr.view_customer(id));
			exit = true;
			System.out.println("\nPress ENTER to continue...");
		}
		wait_for_input();
	}

	public void delete_customer()
	{
		String id = check_id();
		if ((customer_ctr.existing_customer(id))
				&& (customer_ctr.delete_customer(id)))
		{
			System.out
					.println("You successfully deleted a customer! Press ENTER to continue...");
		}
		else
		{
			System.out.println("There is no customer with this ID!");
		}
		wait_for_input();
	}

	private void update_name(String id)
	{
		if (customer_ctr.update_customer(id, input_name(), "", "", "", "", ""))
		{
			System.out
					.println("You successfully changed a customer's name.Press ENTER to continue...");
			wait_for_input();
		}
	}

	private void update_phone_nr(String id)
	{
		Scanner keyboard = new Scanner(System.in);
		if (customer_ctr
				.update_customer(id, "", input_number(), "", "", "", ""))
		{
			System.out
					.println("You successfully changed a customer's phone number.Press ENTER to continue...");
			keyboard.nextLine();
		}
		keyboard.close();
	}

	private void update_address(String id)
	{
		if (customer_ctr.update_customer(id, "", "", input_address(), "", "",
				""))
		{
			System.out
					.println("You successfully changed a customer's address.Press ENTER to continue...");
			wait_for_input();
		}
	}

	private void update_code(String id)
	{
		if (customer_ctr.update_customer(id, "", "", "", input_code(), "", ""))
		{
			System.out
					.println("You successfully changed a customer's postal code.Press ENTER to continue...");
			wait_for_input();
		}
	}

	private void update_city(String id)
	{
		if (customer_ctr.update_customer(id, "", "", "", "", input_city(), ""))
		{
			System.out
					.println("You successfully changed a customer's city.Press ENTER to continue...");
			wait_for_input();
		}
	}

	private void update_group(String id)
	{
		if (customer_ctr.update_customer(id, "", "", "", "", "", input_group()))
		{
			System.out
					.println("You successfully changed a customer's group.Press ENTER to continue...");
			wait_for_input();
		}
	}

	private void add_group()
	{
		boolean ok = true;
		double discount = 0;
		String name = "";
		do
		{
			System.out.println("Please enter Group specifications:\nName:");
			name = wait_for_input();
			if (customer_ctr.is_group(name))
			{
				System.out
						.println("You entered a name that is already existing, please try again!");
				ok = false;
			}
			else
			{
				ok = true;
			}
		} while (!ok);
		ok = true;
		System.out.println("Terms of payment:");
		String terms = wait_for_input();
		if (customer_ctr.add_group(name, terms, input_discount()))
		{
			System.out
					.println("You successfully added a type of group of customers! Press ENTER to continue...");
		}
		else
		{
			System.out
					.println("You did not succeed in adding a group. Press ENTER to continue...");
		}
		wait_for_input();
	}

	private void edit_group()
	{
		boolean ok = true;
		String name = "";
		do
		{
			System.out.println("Name of the group to be edited:");
			name = wait_for_input();
			if (!customer_ctr.is_group(name))
			{
				System.out.println("There is no such group, please try again!");
				ok = false;
			}
			else
			{
				ok = true;
			}
		} while (!ok);
		System.out.println("Terms of delivery:");
		String terms = wait_for_input();
		if (customer_ctr.update_group(name, terms, input_discount()))
		{
			System.out
					.println("You successfully edited a group of customers! Press ENTER to continue...");
		}
		else
		{
			System.out
					.println("You did not succeed in editing a group. Press ENTER to continue...");
		}
		wait_for_input();
	}

	private void view_all()
	{
		System.out.println("***GROUPS***      \n\n");
		System.out.println(customer_ctr.print_groups());
		System.out.println("Press ENTER to continue...");
		wait_for_input();
	}

	private void print_group()
	{
		System.out.println("Name of the group:");
		String name = wait_for_input();
		if (!customer_ctr.is_group(name))
		{
			System.out.println("There is no such group, please try again!");
		}
		else
		{
			System.out.println(customer_ctr.find_group(name));
			System.out.println("Press ENTER to continue...");
		}
		wait_for_input();
	}

	private void delete_group()
	{
		System.out.println("Name of the group:");
		String name = wait_for_input();
		if (customer_ctr.is_group_used(name))
		{
			System.out
					.println("You can not delete this group.\nThere are customers in this group. Press ENTER to conitnue...");
		}
		else if ((customer_ctr.is_group(name))
				&& (customer_ctr.delete_group(name)))
		{
			System.out.println("You successfully deleted Group " + name
					+ "! Press ENTER to continue...");
		}
		else
		{
			System.out
					.println("There is no such group with this name! Press ENTER to continue...");
		}
		wait_for_input();
	}

	private double input_discount()
	{
		Scanner keyboard = new Scanner(System.in);
		boolean ok = true;
		double discount = 0;
		do
		{
			System.out.println("Discount in percentages:");
			String del = wait_for_input();
			if (del.endsWith("%"))
			{
				del = del.substring(0, del.length() - 1);
			}
			try
			{
				discount = Double.parseDouble(del);
				if ((discount < 0) || (discount > 100))
				{
					System.out
							.println("The discount was not correct, please try again!");
					ok = false;
				}
				else
				{
					ok = true;
				}
			}
			catch (Exception e)
			{
				System.out
						.println("The discount was not correct, please try again!");
				ok = false;
			}
		} while (!ok);
		keyboard.close();
		return discount;
	}

	private String check_id()
	{
		boolean ok = false;
		String id = "";
		while (!ok)
		{
			System.out.println("Customer's ID:");
			id = wait_for_input();
			if (!customer_ctr.existing_customer(id))
			{
				System.out.println("Customer with such ID is not found.");
			}
			else
			{
				ok = true;
			}
		}
		return id;
	}

	private String input_name()
	{
		System.out.println("Customer's name:");
		return wait_for_input();
	}

	private String input_number()
	{
		String phone = "";
		String new_phone;
		System.out.println("Customer's phone number:");
		boolean ok = false;
		while (!ok)
		{
			phone = wait_for_input();
			if (phone.matches("\\+?[0-9]+(:?-[0-9]+)*"))
			{
				if (phone.contains("-"))
				{
					new_phone = phone.replaceAll("-", "");
				}
				else
				{
					new_phone = phone;
				}

				if ((new_phone.length() == 8)
						|| (new_phone.length() == 11 && new_phone
								.startsWith("+")))
				{
					ok = true;
				}
				else
				{
					System.out
							.println("The phone was typed incorrectly, type it again:");
				}
			}
			else
			{
				System.out
						.println("The phone was typed incorrectly, type it again:");
			}
		}
		return phone;
	}

	private String input_address()
	{
		System.out.println("Customer's address:");
		return wait_for_input();
	}

	private String input_code()
	{
		String code = "";
		boolean ok = false;
		System.out.println("Customer's postal code:");
		while (!ok)
		{
			code = wait_for_input();
			if (code.matches("[0-9]+") && code.length() == 4)
			{
				ok = true;
			}
			else
			{
				System.out
						.println("The postal code was typed incorrectly, type it again:");
			}
		}
		return code;
	}

	private String input_city()
	{
		System.out.println("Customer's city:");
		return wait_for_input();
	}

	private String input_group()
	{
		String name = "";
		boolean ok = true;
		do
		{
			System.out.println("Enter customer's group:");
			name = wait_for_input();
			if (name.equals("-"))
			{
				view_all();
			}
			else if (!customer_ctr.is_group(name))
			{
				System.out
						.println("There is no such group with this name in the system.\nPlease try again or press (-) to view all groups!");
				ok = false;
			}
			else
			{
				ok = true;
			}
		} while (!ok);
		return name;
	}

	private String wait_for_input()
	{
		Scanner keyboard = new Scanner(System.in);
		String s = keyboard.nextLine();
		keyboard.close();
		return s;
	}
}
